<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 银行模型
 * @author www.dingwei.cn
 */

class MerchantBankModel extends MY_Model{
    public $tbUser = 'merchant';
    public $tbBank = 'merchant_bank_card';
    public $tbAccount = 'merchant_bank_card';

    /**
     * 保存银行
     *
     * @param	array	$data	保存数据
     * @return	boolean
     */
    public function save($data){

        if($id = $this->insert($this->tbBank, $data)){
            return $id;
        }
    }

    /**
     * 获取银行卡信息
     * @param $merchantId
     * @param $accountNumber
     * @return array
     */
    public function getInfoByMerchantIdAndNumber($merchantId,$accountNumber){
        if(!$merchantId || !$accountNumber) return array();

        $this->db->where(array('merchant_id' => $merchantId,'account_number' => $accountNumber));

       return $this->getOne($this->tbBank);
    }

    /**
     * 获取银行卡信息
     * @param $merchantId
     * @param $accountNumber
     * @return array
     */
    public function getInfo($id,$merchantId){
        if(!$id) return array();

        if($merchantId){
            $this->db->where(array('merchant_id' => $merchantId));
        }

        $this->db->where(array('id' => $id));

        return $this->getOne($this->tbBank);
    }

    /**
     * 删除银行卡或者支付宝
     */
    public function delectAccount($id, $merchantId){
        if(!$id)return false;
        $this->db->where(array('id' => $id, 'merchant_id' => $merchantId));
        return $this->delete($this->tbBank);
    }
    /**
     * 获取银行卡 支付宝列表
     * @param $merchantId
     * @param int $type  1:银行卡 2：支付宝
     * @param $page
     * @return array
     */
    public function appGetAccountList($merchantId, $type = 0,$page){

        if(!$merchantId)return array();

        $this->db->select('id,type,account_name,account_number,icon,open_bank')
                 ->where('merchant_id', $merchantId)
                 ->where('type', $type)
                 ->order_by('id desc');

        $tA = $this->appPage($this->tbBank, $page);
        foreach ($tA['list'] as $k=>&$v){
            $v['icon'] = getImage($v['icon'], 'og');

            if($v['type'] == 1){
                //格式化银行卡
                //算算余4多少
                $yu = strlen($v['account_number'])%4;
                if($yu == 0)$yu = 4;
                //长度19 - 4  =  15
                $str = '';
                for ($i=0;$i<strlen($v['account_number']) - $yu;$i++){
                    $str .='*';
                    //每4位一个空格
                    if($i%4 == 3 && $i>0){
                        $str .= ' ';
                    }
                }
                $v['account_number'] = $str.substr($v['account_number'],-$yu);
            }else{//支付宝账号
                if(strpos($v['account_number'],'@')){
                    $arr = explode('@', $v['account_number']);
                    $rest = substr($arr[0], 4, -2);
                    $v['account_number'] = str_replace($rest, str_repeat('*', strlen($rest)), $arr[0]).'@'.$arr[1];
                }elseif (valid_mobile($v['account_number'])){//手机号
                    $v['account_number']=substr($v['account_number'],0,3)."****".substr($v['account_number'],7,4);
                }
            }
        }

        return $tA;
    }

    /**
     * 后台获取用户银行
     *
     * @return array
     */
	public function adminGetList(){

        $merchantId = intval(get('merchant_id'));
        $account_name = trim(get('account_name'));
        $account_number = trim(get('account_number'));
        $mobile= trim(get('mobile'));
        $type = intval(get('bank_type'));//1-银行卡 2-三方

	    if(!$merchantId)return array();

	    if($account_name){
	        $this->db->like(array('b.account_name' => $account_name));
        }

        if($account_number){
            $this->db->like(array('b.account_number' => $account_number));
        }

        if($mobile){
            $this->db->where(array('b.mobile' => $mobile));
        }

        if($type){
            $this->db->where(array('b.type' => $type));
        }
	    $this->db->where(array('b.merchant_id' => $merchantId));

	    $this->db->select('b.id,b.account_name,b.account_number,b.mobile,b.open_bank,b.type')
                 ->join($this->tbUser.' u','u.id = b.merchant_id');

        return $this->pagination($this->tbBank.' b');
    }

    /**
     * 获取账户类型
     * @param 	integer $status
     * @return	string
     */
    public function getBankType($status = null, $show = false){

        $config = array(1 => '银行卡', 2 => '支付宝');
        if( ! $status){
            return $config;
        }

        if($status && ! isset($config[$status])){
            return '';
        }

        if( ! $show){
            return $config[$status];
        }

        return'';
    }


}